# coding=utf-8
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib


class Email():
    def __init__(self, smtp, port, username, password):
        """
        :param smtp: address you email server
        :param port: port you email server
        :param username: user in email server
        :param password: password for user
        """
        self.smtp = smtp
        self.port = port
        self.sender = username
        self.password = password

    def login(self):
        """
        Login to you email server.

        """
        server = smtplib.SMTP(self.smtp, self.port)
        server.ehlo()
        server.starttls()
        server.ehlo()
        server.login(self.sender, self.password)
        server.ehlo()
        self.server = server

    def sendMsg(self, recipient, subject, html):
        """
        Send message with spacial html text.

        :param recipient: who send a letter
        :param subject: subject of letter
        :param html: content of letter
        """
        msg = MIMEMultipart('alternative')
        msg['Subject'] = subject
        msg['From'] = self.sender
        msg['To'] = recipient
        if isinstance(html, unicode):
            html = html.encode('utf-8')
        html = MIMEText(html, 'html')
        msg.attach(html)
        self.server.sendmail(self.sender, recipient, msg.as_string())

